import inspect
import clients
from django.db import models
from django.core.management.base import BaseCommand, CommandError
from clients.models import InlineAwareModel, InlineSettings, InlineSettingsField


class Command(BaseCommand):
    args = ''
    help = 'Gets payments from last time the command was executed'

    def handle(self, *args, **options):
        clsmembers = inspect.getmembers(clients.models, inspect.isclass)
        for cls in clsmembers:
            model = cls[1]
            if issubclass(model, InlineAwareModel) and not model is InlineAwareModel:
                model_name = model._meta.verbose_name.title()
                if (not InlineSettings.objects.filter(name=model_name)):
                    model_settings = InlineSettings(name = model._meta.verbose_name.title())
                    model_settings.save()
                    for field in model._meta.fields + model._meta.many_to_many:
                        if not isinstance(field, models.AutoField):
                            field_settings = InlineSettingsField(name=field.name, verbose=field.verbose_name,
                                                             model_settings=model_settings)
                            field_settings.save()